#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/12/11 22:22
# @Author  : 新猿异码
# @Site    : https://gitee.com/chshao/autotest
# @CSDN    : https://blog.csdn.net/m0_37576542?type=blog
# @File    : emails.py
# @Desc    : 邮件通知模块
# @Software: PyCharm
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication


class Email(object):

    def __init__(self):
        self._smtp_server = None
        self._smtp_port = None
        self._email_user = None
        self._email_pwd = None
        self._email_receiver = None
        self._server_obj = None

    @property
    def smtp_server(self):
        return self._smtp_server

    @smtp_server.setter
    def smtp_server(self, smtp_server: str):
        self._smtp_server = smtp_server

    @property
    def smtp_port(self):
        return self._smtp_port

    @smtp_port.setter
    def smtp_port(self, smtp_port: str):
        self._smtp_port = smtp_port

    @property
    def email_user(self):
        return self._email_user

    @email_user.setter
    def email_user(self, email_user: str):
        self._email_user = email_user

    @property
    def email_pwd(self):
        return self._email_pwd

    @email_pwd.setter
    def email_pwd(self, email_pwd: str):
        self._email_pwd = email_pwd

    @property
    def email_receiver(self):
        return self._email_receiver

    @email_receiver.setter
    def email_receiver(self, email_receiver: str):
        self._email_receiver = email_receiver

    def login(self):
        if None in [self.smtp_server, self.smtp_port, self.email_user, self.email_pwd]:
            print("please check your email server and user message.")
            return -1
        try:
            self._server_obj = smtplib.SMTP(self.smtp_server, self.smtp_port)
        except ConnectionRefusedError:
            print("Connection smtp server refused")
            return -2
        self._server_obj.login(self.email_user, self.email_pwd)
        return 0

    def send_message(self, subject: str, mime_text: str, attach_files: list):
        """
        发送邮件内容
        :param subject: 邮件主题
        :param mime_text: 邮件正文
        :param attach_files: 附件文件
        :return:
        """
        message = MIMEMultipart()
        message["From"] = self.email_user
        message["To"] = self.email_receiver
        message["Subject"] = subject
        message.attach(MIMEText(mime_text))
        for file in attach_files:
            if os.path.exists(file):
                attachment = MIMEApplication(open(file, "rb").read())
                attachment.add_header("Content-Disposition", "attachment", filename=os.path.split(file)[1])
                message.attach(attachment)
        self._server_obj.sendmail(self.email_user, self.email_receiver, message.as_string())
        self._server_obj.quit()


if __name__ == '__main__':
    email = Email()
    email.connect("localhost", 567)
    email.smtp_server = "773746130@qq.com"
    print(email.smtp_server)
